package com.hunt.controller;

import com.hunt.model.dto.PageInfo;
import com.hunt.model.dto.UserLevel;
import com.hunt.model.entity.UserLevel0;
import com.hunt.service.SystemService;
import com.hunt.service.UserLevelService;
import com.hunt.util.ResponseCode;
import com.hunt.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author ouyangan
 * @Date 2016/10/8/13:37
 * @Description
 */
@Api(value = "用户级别管理模块")
@Controller
@RequestMapping("userlevel")
public class UserLevelController extends BaseController {

    @Autowired
    private UserLevelService userLevelService;

    @Autowired
    private SystemService systemService;

    @ApiOperation(value = "跳转至用户级别管理模块", httpMethod = "GET", produces = "text/html")
    @RequestMapping(value = "userlevel", method = RequestMethod.GET)
    public String skipUserLevel() {

        return "system/userlevel";
    }

    /**
     * 更新用户Level
     * @param userLevel
     * @return
     */
    @ApiOperation(value = "更新用户级别", httpMethod = "POST", produces = "application/json", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public Result update(UserLevel userLevel) {
        try {
            if(userLevelService.update(userLevel) == 0){
                return Result.error();
            }
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     *新增用户级别数据
     * @param userlevel
     * @param cgDx
     * @param cgZx
     * @param cgLn
     * @param cgNx
     * @param eTime
     * @return
     */
    @ApiOperation(value = "新增用户级别数据", httpMethod = "POST", produces = "application/json", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public Result insert(@RequestParam String userlevel,
                         @RequestParam(defaultValue = "0", required = false) int cgDx,
                         @RequestParam(defaultValue = "0", required = false) int cgZx,
                         @RequestParam(defaultValue = "0", required = false) int cgLn,
                         @RequestParam(defaultValue = "0", required = false) int cgNx,
                         @RequestParam(defaultValue = "0", required = false) int eTime) {
        try {
            UserLevel userLevel = new UserLevel();
            userLevel.setUserlevel(userlevel);
            userLevel.setCgdx(cgDx);
            userLevel.setCgzx(cgZx);
            userLevel.setCgln(cgLn);
            userLevel.setCgnx(cgNx);
            userLevel.setEtime(eTime);
            int recode = userLevelService.insertUserLevel(userLevel);
            return Result.success(recode);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }



    /**
     * 通过UserLevel删除用户级别
     * @param userlevel
     * @return
     */
    @ApiOperation(value = "删除用户级别", httpMethod = "GET", produces = "application/json", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "delete", method = RequestMethod.GET)
    public Result delete(@RequestParam String userlevel) {

        try {
            UserLevel userLevel = userLevelService.selectByUserLevel(userlevel);
            if (userLevel == null) {
                return Result.error(ResponseCode.data_not_exist.getMsg());
            }
            userLevelService.deleteByUserlevelId(userlevel);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return Result.error(ResponseCode.error.getMsg());
        }
    }


    /**
     * 通过UserLevel对象查询单个用户级别
     * @param userLevel
     * @return
     */
    @ApiOperation(value = "通过UserLevel对象查询单个用户级别", httpMethod = "GET", produces = "application/json", response = Object.class)
    @ResponseBody
    @RequestMapping(value = "selectOneByObj", method = RequestMethod.GET)
    public Object selectOne(@RequestParam UserLevel userLevel) {

        UserLevel ul =null;
        try {
             ul = userLevelService.selectUserLevel(userLevel);
            if (ul == null) {
                return "";
            }
            return  ul;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return Result.error(ResponseCode.error.getMsg());
        }
    }
    /**
     * 通过UserLevel标识查询单个用户级别对象
     * @param userlevel
     * @return
     */
    @ApiOperation(value = "通过UserLevel标识查询单个用户级别", httpMethod = "GET", produces = "application/json", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "selectOne", method = RequestMethod.GET)
    public Result selectOneByUl(@RequestParam String userlevel) {
            UserLevel userLevel  = userLevelService.selectByUserLevel(userlevel);
        return Result.success(userLevel);
    }


    /**
     * 查询用户列表分页
     * @return
     *   @RequiresPermissions("user:list")
     */
    @ApiOperation(value = "分页查询用户级别列表", httpMethod = "GET", produces = "application/json", response = PageInfo.class)
    //@RequiresPermissions("user:list")
    @ResponseBody
    @RequestMapping(value = "listByPage", method = RequestMethod.GET)
    public PageInfo listByPage(@RequestParam(defaultValue = "1") int pageNo,
                               @RequestParam(defaultValue = "30") int pageSize,
                               @RequestParam(defaultValue = "asc") String order) {

        PageInfo pageInfo = userLevelService.selectPage(pageNo, pageSize, order);
        return pageInfo;
    }
    /**
     * 查询用户列表分页
     * @return
     *   @RequiresPermissions("user:list")
     */
    @ApiOperation(value = "分页查询用户级别列表", httpMethod = "GET", produces = "application/json", response = PageInfo.class)
    //@RequiresPermissions("user:list")
    @ResponseBody
    @RequestMapping(value = "listByPage0", method = RequestMethod.GET)
    public PageInfo listByPage0(@RequestParam(defaultValue = "1") int pageNo,
                               @RequestParam(defaultValue = "30") int pageSize,
                               @RequestParam(defaultValue = "asc") String order) {
        //因为前端开号页和级别管理页冲突,需要把userlevel字段名换成userlevel0
        PageInfo pageInfo = userLevelService.selectPage(pageNo, pageSize, order);
        List< UserLevel>   userLevels= (List<UserLevel>) pageInfo.getRows();
        List<UserLevel0> userLevel0s=new ArrayList<>();

        for(UserLevel u:userLevels){
            UserLevel0 u0=new UserLevel0();
            BeanUtils.copyProperties(u,u0);
            u0.setUserlevel0(u.getUserlevel());
            userLevel0s.add(u0);
        }
         pageInfo.setRows(userLevel0s);

        return pageInfo;
    }
    /**
     * 查询用户列表不分页
     * @return
     */
    @ApiOperation(value = "查询用户级别列表", httpMethod = "GET", produces = "application/json", response = PageInfo.class)
    //@RequiresPermissions("user:list")
    @ResponseBody
    @RequestMapping(value = "listNoPage", method = RequestMethod.GET)
    public PageInfo listALL() {
        List<UserLevel> userLevels = userLevelService.selectAll();
        PageInfo pageInfo = new PageInfo(0,userLevels);
        return pageInfo;
    }


}
